from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
from .models import *
from .serializer import *
# Create your views here.
# 分液器
class Mypagination(PageNumberPagination):
    page_size = 3
    page_query_param = 'page'
    page_size_query_param = 'pagesize'


# 优惠券的视图类
class CouponsViewSet(ModelViewSet):
    queryset = Coupon.objects.all()
    serializer_class = CouponSer
    # pagination_class = Mypagination
    lookup_url_kwarg = 'pk'
    lookup_field = 'pk'

    # def create(self, request, *args, **kwargs):
    #     name = request.data.get('name')
    #     coupon_type = request.data.get('coupon_type')
    #
    #     coupon = Coupon.objects.filter(name=name,coupon_type_id=coupon_type).first()
    #
    #     if coupon:
    #         return Response({'msg':'该优惠券已经添加'},status=400)
    #     else:
    #         ser = self.get_serializer(data=request.data)
    #         ser.is_valid()
    #         ser.save()
    #         return Response({'msg': '添加成功'}, status=201)


# 优惠券类型的视图
class CouponTypeAPIView(ListAPIView):
    queryset = CouponType.objects.all()
    serializer_class = CouponTypeSer

